﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Schema = QASPRibbon.RibbonSchema;

namespace QASPRibbon.Controls
{
    /// <summary>
    /// Represents a Tab control in the SharePoint Server Ribbon. 
    /// </summary>
    public class RibbonTab : RibbonElement
    {
        #region Properties

        /// <summary>
        /// Gets or sets collection of groups.
        /// </summary>
        public IEnumerable<RibbonGroup> Groups { get; set; }

        public string TemplatesXml
        {
            get { return string.Join(string.Empty, Groups.Select(g => g.TemplateXml).ToArray()); }
        }

        /// <summary>
        /// Gets or sets name of the CSS class (such as "ms-browseTab") to apply when the tab is rendered.
        /// </summary>
        public string CssClass
        {
            get { return _cssClass; }
            set { _cssClass = value; }
        }

        public string ClientId
        {
            get
            {
                return "Ribbon." + Id;
            }
        }

        #endregion

        #region JavaScript

        /// <summary>
        /// Javascript code that executes when a Tab is activated.
        /// </summary>
        public string OnClientActivate { get; set; }


        #endregion

        #region Constructors

        /// <summary>
        /// Creates and initializes a new instance of the RibbonTab.
        /// </summary>
        public RibbonTab() { }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonTab.
        /// </summary>
        /// <param name="id">Identifier</param>
        public RibbonTab(string id) : base(id) { }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonTab.
        /// </summary>
        /// <param name="id">Identifier</param>
        /// <param name="title">Title</param>
        public RibbonTab(string id, string title) : base(id, title) { }

        #endregion

        #region Internal

        internal override string XmlTag { get { return Schema.Tab; } }

        internal IEnumerable<RibbonControl> AllControls
        {
            get
            {
                var res = new List<RibbonControl>();
                res.AddRange(Groups.SelectMany(g => g.AllControls));
                return res;
            }
        }

        internal override XElement AsXElement()
        {
            var tabElement =
                new XElement(XmlTag,
                    new XAttribute(Schema.Id, "Ribbon." + Id),
                    new XAttribute(Schema.Title, Title),
                    new XAttribute(Schema.CssClass, CssClass),
                    new XAttribute(Schema.Sequence, Sequence),
                    new XElement(Schema.Scaling, new XAttribute(Schema.Id, "Ribbon." + Id + ".Scaling")),
                    new XElement(Schema.Groups, new XAttribute(Schema.Id, "Ribbon." + Id + ".Groups"))
                );
            var groupIndex = 0;
            foreach (var group in Groups)
            {
                groupIndex++;
                group.NameSpace = FullId;
                group.BuildTemplate();
                var groupElement = group.AsXElement(Id, groupIndex);
                tabElement.Element(Schema.Scaling).AddFirst(groupElement.Element(Schema.Scaling).Element(Schema.MaxSize));
                tabElement.Element(Schema.Scaling).Add(groupElement.Element(Schema.Scaling).Elements(Schema.Scale).ToArray());
                tabElement.Element(Schema.Groups).Add(groupElement.Element(Schema.Group));
            }

            return tabElement;
        }

        internal string JSDefinition
        {
            get { return string.Empty; }
        }

        #endregion

        #region Private

        private string _cssClass = string.Empty;

        #endregion
    }

    internal class RibbonTabEventArgs : EventArgs
    {
        public RibbonTab Instance;

        public RibbonTabEventArgs(RibbonTab tab)
        {
            Instance = tab;
        }
    }
}
